package com.hsxshop.controller;

import com.hsxshop.model.User;
import com.hsxshop.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * Created by wangchongya on 17/5/12.
 */
@Controller
@RequestMapping(value = "/home")
public class HomeController {

    @Resource(name = "userService")
    private UserService us;

    @RequestMapping(value ="/home", method = RequestMethod.GET)
    public String home(){
        System.out.print("121313");

        return "index";
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(){

        return "login";
    }

    @RequestMapping(value = "doLogin", method = RequestMethod.POST)
    public String doLogin(User user, HttpSession s, Model m){
//
//        User u=new User();
//        u.setName("wwww111");
//        u.setNickname("222");
//        u.setPassword("333");
//        u.setEasemobPwd("44444");
//        u.setPhone("13022516066");
//        u.setIDNumber("11111");
//        u.setAvatar("1233");
//        u.setCreateTime(new Date());
//        u.setIdentity("23124321");
//        u.setSignature("signature21312");
//        u.setUser_number("2312412");
//
//        System.out.println("us:"+us.toString());
//
//        us.saveEntity(u);

        System.out.println("us:"+us.toString());


        String hql="from User u where u.phone=?";
        List<User> users= us.findByHQL(hql,user.getPhone());

        if(users==null || users.isEmpty()){
            m.addAttribute("error","手机号码错误");
        }else{
            User u=users.get(0);
            s.setAttribute("phone",u.getPhone());

        }

        System.out.print("提交登录信息");

        return "index";
    }

}
